package com.zhaoxi.springboot.restful.springbootrestful.service;

import com.mmren.edu.restful.entity.User;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * 欢迎来到牧码人教育，做Java我们是专业的
 *
 * @创建人： 牧码人教育-Gerry
 * @创建时间： 2020-4-26
 * @功能描述：
 */
@Service
public class UserServiceImpl implements UserService {
    private List<User> users = new CopyOnWriteArrayList<>();
    @Override
    public boolean save(User user) {
        return this.users.add(user);
    }

    @Override
    public boolean remove(Integer id) {
        User u = queryUserById(id);
        return this.users.remove(u);
    }

    @Override
    public boolean update(User user) {
        User u = queryUserById(user.getId());
        System.out.println("update query user: "+u);
        this.users.remove(u);
        return this.users.add(user);
    }

    @Override
    public User queryUserById(Integer id) {
        // lambda表达式 ->
        Optional<User> first = users.stream().filter(u -> u.getId().equals(id)).findFirst();
        return first.get();
    }

    @Override
    public List<User> queryAll() {
        return users;
    }
}
